import { PrismaClient } from '@prisma/client'
import bcrypt from 'bcrypt'

const prisma = new PrismaClient()

async function main() {
  // 清除现有数据
  await prisma.user.deleteMany()
  await prisma.region.deleteMany()

  // 创建测试用户
  const users = [
    {
      email: 'admin@example.com',
      password: await bcrypt.hash('123456', 10)
    },
    {
      email: 'user@example.com',
      password: await bcrypt.hash('123456', 10)
    }
  ]

  for (const user of users) {
    await prisma.user.create({
      data: user
    })
  }

  // 添加地区测试数据
  const regions = [
    // 市级
    { code: '3301', name: '杭州市', level: 1, parentCode: null },
    { code: '3302', name: '宁波市', level: 1, parentCode: null },
    
    // 县级
    { code: '330106', name: '西湖区', level: 2, parentCode: '3301' },
    { code: '330104', name: '江干区', level: 2, parentCode: '3301' },
    { code: '330203', name: '海曙区', level: 2, parentCode: '3302' },
    
    // 街道
    { code: '330106001', name: '蒋村街道', level: 3, parentCode: '330106' },
    { code: '330106002', name: '转塘街道', level: 3, parentCode: '330106' },
    { code: '330104001', name: '笕桥街道', level: 3, parentCode: '330104' },
    
    // 村居
    { code: '330106001001', name: '蒋村社区', level: 4, parentCode: '330106001' },
    { code: '330106001002', name: '永福社区', level: 4, parentCode: '330106001' },
    { code: '330106002001', name: '转塘村', level: 4, parentCode: '330106002' }
  ]

  // 创建地区数据
  for (const region of regions) {
    await prisma.region.create({
      data: region
    })
  }

  console.log('种子数据已创建')
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  }) 